package com.zzz.bookdemo.controller;

/*
 * @author zzr
 * @date: 2025/09/16  19:40
 * @description:
 */

import com.zzz.bookdemo.constants.Constants;
import com.zzz.bookdemo.model.UserInfo;
import com.zzz.bookdemo.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.graphql.GraphQlProperties;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",produces = "application/json")
    public boolean login(String name, String password, HttpSession session) {
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
            return false;
        }

        UserInfo userInfo = userService.queryUserByName(name);

        // 验证用户是否存在
        if(null == userInfo) {
            return false;
        }

        // 验证密码是否正确
        if(userInfo != null && password.equals(userInfo.getPassword())) {
            // 密码正确
            // 存储在 Session 中
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY,userInfo);
            return true;
        }
        // 账号密码错误
        return false;
    }
}
